package org.jeecg.modules.dd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.dd.mapper.CnDakaMapper;
import org.jeecg.modules.dd.model.CnDaka;
import org.jeecg.modules.dd.model.Project;
import org.jeecg.modules.dd.service.CnDakaService;
import org.jeecg.modules.dd.service.ProjectService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dd")
@Component
public class CnDakaController {

    @Resource
    private CnDakaService cnDakaService;
    @Resource
    private ProjectService projectService;

    //    新增
    @PostMapping("/add")
    public Result<?> add(@RequestBody CnDaka cnDaka) {

        LambdaQueryWrapper<CnDaka> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(CnDaka::getName, cnDaka.getName());
        objectLambdaQueryWrapper.eq(CnDaka::getProject, cnDaka.getProject());
        objectLambdaQueryWrapper.eq(CnDaka::getDate, LocalDate.now());
        CnDaka one = cnDakaService.getOne(objectLambdaQueryWrapper);
        if (one != null) {
            cnDaka.setId(one.getId());
            cnDaka.setDaTime(new Date());
            cnDaka.setDate(LocalDate.now());
            cnDakaService.updateById(cnDaka);
        } else {
            cnDaka.setDaTime(new Date());
            cnDaka.setDate(LocalDate.now());
            cnDakaService.save(cnDaka);
        }
        return Result.ok("打卡成功");
    }


//    获取所有项目
    @GetMapping("/getProject")
    public Result<?> getProject() {
        List<Project> list = projectService.list();
        return Result.ok(list);
    }



    //    excel导出
    @GetMapping(value = "/export")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        LambdaQueryWrapper<CnDaka> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(CnDaka::getDate, LocalDate.now());
        List<CnDaka> pageList = cnDakaService.list(objectLambdaQueryWrapper);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "创尼电子科技技术有限公司");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, CnDaka.class);
        //自定义导出字段 		   mv.addObject(NormalExcelConstants.EXPORT_FIELDS,"name,keyWord,punchTime");
        //自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("创尼电子科技技术有限公司", "创尼电子科技技术有限公司"));
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }
}
